﻿using System;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Volo.Abp;
using SystemText = System.Text.Json;

namespace Yyd.Share
{
    /// <summary>
    /// 
    /// </summary>
    public class SysTxtIdToStringConverter : SystemText.Serialization.JsonConverter<long>
    {
        public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {


            if (reader.TokenType == JsonTokenType.Number)
            {
                return reader.GetInt64();
            }

            var stringValue = reader.GetString();
            if (string.IsNullOrWhiteSpace(stringValue)) return 0;

            if (long.TryParse(stringValue, out long value))
            {
                return value;
            }

            throw new UserFriendlyException($" 【 {reader.GetString()} 】格式不正确，无法转换成数字");
        }

        public override bool CanConvert(Type objectType)
        {
            return typeof(long).Equals(objectType);
        }



        public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString());
        }
    }


}
